package controllers.user;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ProjectService;
import services.RegistrationService;
import services.UserService;
import services.WorkUpdateService;
import controllers.AbstractController;
import domain.Project;
import domain.Registration;
import domain.Vacancy;
import domain.WorkUpdate;
import forms.ProjectForm;

@Controller
@RequestMapping("/workUpdate/user")
public class WorkUpdateUserController extends AbstractController{
	
		// Services
		@Autowired
		private WorkUpdateService workUpdateService;
		@Autowired
		private RegistrationService registrationService;
		@Autowired
		private UserService userService;
		@Autowired
		private ProjectService projectService;
		
		// Constructors
		public WorkUpdateUserController() {
			super();
		}
		
		// Creation
		@RequestMapping(value = "/create", method = RequestMethod.GET)
		public ModelAndView create(@RequestParam int projectId) {
			ModelAndView result;
			Registration registration;
			WorkUpdate workUpdate;
			
			registration = registrationService.getRegistrationForProject(userService.findByPrincipal().getId(), projectId);
			workUpdate = workUpdateService.create(registration);
			workUpdate.setRegistration(registration);

			result = new ModelAndView("workUpdate/edit");
			result.addObject("workUpdate", workUpdate);
			result.addObject("projectId", projectId);
		
			return result;

		}
		
		
		// Edition
		@RequestMapping(value = "/create", method = RequestMethod.POST, params = "save")
		public ModelAndView save(@Valid WorkUpdate workUpdate, BindingResult binding) {
			ModelAndView result;
			Project project;
			int projectId;
			ProjectForm projectForm;
			Collection<WorkUpdate> workUpdates;
			Collection<Registration> registrations;
			Boolean hasVacancies;
			Boolean isNew;
			Boolean editable;
			Boolean isOwner;
			Boolean creating;
			Boolean recruiting;
			Boolean canJoin;
			Boolean started;
			Boolean joined;
			Boolean following;
			Boolean finished;

			if (binding.hasErrors()) {
				result = createEditModelAndView(workUpdate);
			} else {
				try {
					workUpdateService.save(workUpdate);
					result = new ModelAndView("project/edit");
				} catch (Throwable oops) {
					result = createEditModelAndView(workUpdate, "workUpdate.commit.error");
				}
			}
			project = workUpdate.getRegistration().getProject();
			projectId = project.getId();
			Collection<Vacancy> vacancies = project.getVacancies();
			
			projectForm = projectService.projectToForm(project);
			workUpdates = workUpdateService.findByProjectId(projectId);
			registrations = registrationService.findByProjectId(project.getId());

			isNew = false;
			editable = false;
			isOwner = project.getCreator().equals(userService.findByPrincipal());
			creating = project.getStatus().equals("Creating");
			hasVacancies = vacancies.isEmpty();
			recruiting = project.getStatus().equals("Recruiting");
			started = project.getStatus().equals("Started");
			canJoin = projectService.findAvailableProjects().contains(project);
			joined = project.getRegistrations().contains(
					registrationService.getRegistrationForProject(userService
							.findByPrincipal().getId(), projectId));
			following = project.getFollowers().contains(
					userService.findByPrincipal());
			finished = project.getStatus().equals("Finished");
			
			
			
			result.addObject("projectId", projectId);
			result.addObject("projectForm", projectForm);
			result.addObject("workUpdates", workUpdates);
			result.addObject("registrations", registrations);
			result.addObject("requestURI", "project/user/edit.do");
			result.addObject("isNew", isNew);
			result.addObject("editable", editable);
			result.addObject("hasVacancies", hasVacancies);
			result.addObject("isOwner", isOwner);
			result.addObject("creating", creating);
			result.addObject("recruiting", recruiting);
			result.addObject("started", started);
			result.addObject("finished", finished);
			result.addObject("canJoin", canJoin);
			result.addObject("joined", joined);
			result.addObject("following", following);
			result.addObject("projectId", projectId);
			result.addObject("backURI", "index");
			return result;
		}
		
		// Ancillary methods
		protected ModelAndView createEditModelAndView(WorkUpdate workUpdate) {
			ModelAndView result;
			result = createEditModelAndView(workUpdate, null);
			return result;

		}

		protected ModelAndView createEditModelAndView(WorkUpdate workUpdate,
				String message) {
			ModelAndView result;
			result = new ModelAndView("workUpdate/edit");
			result.addObject("workUpdate", workUpdate);
			result.addObject("message", message);

			return result;
		}
		
}
